package net.zigin.nhi.could.waste.util;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.zigin.nhi.could.waste.exception.EpiCloudException;

public class EncryptDataUtil {

	private static final String secret = "YLFWZXZSGLXT";

	public static class EncryptData {
		private String type;
		private String data;
		public String getType() {
			return type;
		}
		public void setType(String type) {
			this.type = type;
		}
		public String getData() {
			return data;
		}
		public void setData(String data) {
			this.data = data;
		}
	}
	
	/**
	 * 加密数据
	 * @param type
	 * @param data
	 * @return
	 */
	public static String encryptData(String type, String data) {
		try {
			EncryptData encryptData  = new EncryptData();
			encryptData.setType(type);
			encryptData.setData(data);
			Gson gson = new GsonBuilder().create();
			return DESUtil.encrypt(secret, gson.toJson(encryptData));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 解密数据
	 * @param encryptData
	 * @return
	 */
	public static EncryptData decryptData(String encryptData) {
		try {
			String json = DESUtil.decrypt(secret, encryptData);
			Gson gson = new GsonBuilder().create();
			EncryptData data = gson.fromJson(json, EncryptData.class);
			if(data == null){
				throw new EpiCloudException("二维码解析错误！");
			}
			return data;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EpiCloudException("二维码解析错误！");
		}
	}
	
}
